package com.hzncc.flowable_diboot.service.impl;

import com.diboot.core.service.impl.BaseServiceImpl;
import com.diboot.iam.util.IamSecurityUtils;
import com.hzncc.flowable_diboot.entity.Business;
import com.hzncc.flowable_diboot.entity.BusinessFollow;
import com.hzncc.flowable_diboot.entity.ToDoList;
import com.hzncc.flowable_diboot.mapper.BusinessFollowMapper;
import com.hzncc.flowable_diboot.mapper.BusinessMapper;
import com.hzncc.flowable_diboot.service.BusinessFollowService;
import com.hzncc.flowable_diboot.service.BusinessService;
import com.hzncc.flowable_diboot.service.ToDoListService;
import com.hzncc.flowable_diboot.util.ConstantUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;


/**
* 商机跟进 相关Service实现类
* @author MyName
* @version 1.0
* @date 2024-06-12
* Copyright © MyCorp
*/
@Slf4j
@Service
@RequiredArgsConstructor
public class BusinessFollowServiceImpl extends BaseServiceImpl<BusinessFollowMapper, BusinessFollow> implements BusinessFollowService {

    private final BusinessService businessService;
    private final ToDoListService toDoListService;
    private final BusinessMapper businessMapper;

    @Override
    public void follow(BusinessFollow businessFollow) {
        businessFollow.setUid(IamSecurityUtils.getCurrentUserId());
        //businessFollow.setCode(codeRuleService.generateCode(ConstantUtil.CODE_RULE_BUSINESS_FOLLOW));
        //createEntity(businessFollow);
        Business entity = businessService.getEntity(businessFollow.getBusinessId());
        entity.setLastFollowUpTime(LocalDateTime.now());
        //businessService.updateEntity(entity);
        // 生成待办
        if (businessFollow.getNextConnectTime() != null) {
            toDoListService.createToDo(new ToDoList().setModel(ConstantUtil.MODEL_SALES)
                    .setBusinessId(entity.getId())
                    .setExecutor(IamSecurityUtils.getCurrentUserId())
                    .setType(ConstantUtil.TODAY_BUSINESS)
                    .setDisTime(LocalDateTime.now())
                    .setMission(String.format(ConstantUtil.BUSINESS_FOLLOW_MSG_TEMPLATE, entity.getName()))
                    .setContactWay(businessFollow.getContactWay())
                    .setStartTime(businessFollow.getNextConnectTime()));
        }
        baseMapper.insert(businessFollow);
        businessMapper.updateById(entity);
    }
}